package com.alks.function.controller.stockdept.pcfactorystockquery;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.ObjectToMapUtil;
import com.alks.common.utils.excelUtils.ExcelPoiUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.common.utils.excelUtils.nestExcel.NestExcelUtil;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.request.stockdept.pcfactorystockwork.ReportInStockQueryRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.ReportStockInQueryRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.ReportStockOutQueryRequest;
import com.alks.function.data.vo.stock.InvoicesStockVO;
import com.alks.function.data.vo.stock.ReportStockInVO;
import com.alks.function.data.vo.stock.ReportStockOutVO;
import com.alks.function.data.vo.stock.ReportStockVO;
import com.alks.function.service.stockdept.pcfactorystockquery.IStockWorkReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: chen
 * @date: 2024/11/14
 * @description: 仓储作业报表
 */
@RestController
@ApiRestController("/stockWork/report")
@Api(value = "仓储作业-报表相关", tags = "仓储作业-报表相关")
@Slf4j
@RoleCheck(prgIds = {"160305", "160306", "160307"})
public class StockWorkReportController {

    @Resource
    private IStockWorkReportService service;

    private static final int NUMBER_SCALE = 0;

    @WebLogger
    @PostMapping("/stockIn")
    @ApiOperation(value = "成品入库明细列表", tags = "仓储作业-报表相关")
    public ResponseInfo reportStockIn(@RequestBody ReportStockInQueryRequest request) {
        return ResponseInfo.ok(service.reportStockIn(request));
    }

    @WebLogger
    @PostMapping("/stockIn/export")
    @ApiOperation(value = "成品入库明细导出", tags = "仓储作业-报表相关")
    public void reportStockInExport(@RequestBody ReportStockInQueryRequest request, HttpServletResponse response) {
        request.setPageNum(1);
        request.setPageSize(Integer.MAX_VALUE);
        List<String> heads = new ArrayList<>();
        heads.add("成品入库明细");
        heads.add(String.format("入库日期：%s至%s",
                    LocalDateTimeUtil.format(request.getBeginTime(), "yyyy-MM-dd"),
                    LocalDateTimeUtil.format(request.getEndTime(), "yyyy-MM-dd")));
        new NestExcelUtil<ReportStockInVO>().exportData(service.reportStockIn(request).getRows(),
                Collections.singletonList(heads), "成品入库明细", response);
    }

    @WebLogger
    @GetMapping("/stockIn/invoices")
    @ApiOperation(value = "成品出入库单导出", tags = "仓储作业-报表相关")
    public void detailExport(@RequestParam String date, @RequestParam Integer type, HttpServletResponse response) {
        TemplateExportParams templateParams = null;
        String fileName = null;
        String collectType = null;
        if (1 == type) {
            templateParams = new TemplateExportParams("templates/invoices_stock_in.xlsx", true);
            fileName = "成品入库单";
            collectType = "I";
        } else if (2 == type) {
            templateParams = new TemplateExportParams("templates/invoices_stock_out.xlsx", true);
            fileName = "成品出库单";
            collectType = "O";
        } else {
            throw new ServiceErrorException("导出参数错误!");
        }
        InvoicesStockVO vo = service.invoiceStock(date, collectType);

        templateParams.setColForEach(true);
        try {
            ExcelUtils.setResponseZF(response, fileName +"_" + date +".xlsx");
            ExcelPoiUtils.writeExcelByTemplateWithImages(response, templateParams, ObjectToMapUtil.convert(vo, "yyyy-MM-dd", NUMBER_SCALE), null);

        } catch (Exception e) {
            throw new ServiceErrorException(fileName + "导出失败!");
        }
    }

    @WebLogger
    @PostMapping("/stockOut")
    @ApiOperation(value = "成品出库明细列表", tags = "仓储作业-报表相关")
    public ResponseInfo reportStockOut(@RequestBody ReportStockOutQueryRequest request) {
        return ResponseInfo.ok(service.reportStockOut(request));
    }

    @WebLogger
    @PostMapping("/stockOut/export")
    @ApiOperation(value = "成品出库明细导出", tags = "仓储作业-报表相关")
    public void reportStockOutExport(@RequestBody ReportStockOutQueryRequest request, HttpServletResponse response) {
        request.setPageNum(1);
        request.setPageSize(Integer.MAX_VALUE);
        List<String> heads = new ArrayList<>();
        heads.add("成品出库明细");
        heads.add(String.format("出库日期：%s至%s",
                LocalDateTimeUtil.format(request.getBeginTime(), "yyyy-MM-dd"),
                LocalDateTimeUtil.format(request.getEndTime(), "yyyy-MM-dd")));
        new NestExcelUtil<ReportStockOutVO>().exportData(service.reportStockOut(request).getRows(),
                Collections.singletonList(heads), "成品出库明细", response);
    }

    @WebLogger
    @PostMapping("/inStock")
    @ApiOperation(value = "成品库存明细列表", tags = "仓储作业-报表相关")
    public ResponseInfo reportInStock(@RequestBody ReportInStockQueryRequest request) {
        return ResponseInfo.ok(service.reportInStock(request));
    }

    @WebLogger
    @PostMapping("/inStock/export")
    @ApiOperation(value = "成品出库明细导出", tags = "仓储作业-报表相关")
    public void reportStockOutExport(@RequestBody ReportInStockQueryRequest request, HttpServletResponse response) {
        request.setPageNum(1);
        request.setPageSize(Integer.MAX_VALUE);
        List<String> heads = new ArrayList<>();
        heads.add("成品库存明细");
        heads.add(String.format("报表日期：%s", LocalDateTimeUtil.format(LocalDate.now(), "yyyy-MM-dd")));
        new NestExcelUtil<ReportStockVO>().exportData(service.reportInStock(request).getRows(),
                Collections.singletonList(heads), "成品库存明细", response);
    }
}
